﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StockLedger.aspx.cs" Culture="en-GB" Inherits="Main_Reports_Inventory_StockInformation_StockLedger" %>

<%@ Register Src="~/User Controls/PageHeading.ascx" TagName="heading" TagPrefix="acc" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Stock Ledgers</title>
    <link href="../../../../Styles/accounts.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" language="javascript">

        function expandDetailsInGrid(imgId, grdId) {
            var imgelem = document.getElementById(imgId);
            var rowdetelem = document.getElementById(grdId);
         
            if (imgelem.alt == "plus") {
                imgelem.src = "../../../../Images/minus.png";
                imgelem.alt = "minus";
                rowdetelem.style.display = "block";
            }
            else {
                imgelem.src = "../../../../Images/plus.png";
                imgelem.alt = "plus";
                rowdetelem.style.display = "none";
            }
            return false;
        }
        </script>
        <script type="text/javascript" language="javascript">
        function checkdate() {
            if (document.getElementById("txtfrom").value == '') {
                alert('Specify From date!');
                document.getElementById("txtfrom").focus();
                return false;
            }
            else if (document.getElementById("txtto").value == '') {
                alert('Specify To Date!');
                document.getElementById("txtto").focus();
                return false;
            }
            else {
                var strFromDate = document.getElementById("txtfrom").value;
                var strToDate = document.getElementById("txtto").value;
                var dtFrom = parseInt(strFromDate.substring(0, 2), 10);
                var monFrom = parseInt(strFromDate.substring(3, 5), 10);
                var yrFrom = parseInt(strFromDate.substring(6, 10), 10);
                var dtTo = parseInt(strToDate.substring(0, 2), 10);
                var monTo = parseInt(strToDate.substring(3, 5), 10);
                var yrTo = parseInt(strToDate.substring(6, 10), 10);
                var dateFrom = new Date(yrFrom, monFrom, dtFrom);
                var dateTo = new Date(yrTo, monTo, dtTo);
                if (dateTo < dateFrom) {
                    alert("from date cannot be greater than to date");
                    document.getElementById("txtfrom").focus();
                    return false;
                }
                else {
                    return true;
                }
            }
        }
        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        function Validation() {
            var flag = true;
            var txtFromdate = document.getElementById("txtFromDate").id;
            var txtTodate = document.getElementById("txtToDate").id;
            if (document.getElementById("txtFromDate").value == '') {
                alert('Specify From date');
                flag = false;
            }
            else if (document.getElementById("txtToDate").value == '') {
                alert('Specify To date');
                flag = false;
            }
            else {
                if (isDate(txtFromdate) && isDate(txtTodate)) {
                    var dateArray = new Array();
                    dateArray = document.getElementById("txtFromDate").value.split("/");
                    var day1 = dateArray[0];
                    var month1 = dateArray[1];
                    var year1 = dateArray[2];
                    dateArray = document.getElementById("txtToDate").value.split("/");
                    var day2 = dateArray[0];
                    var month2 = dateArray[2];
                    var year2 = dateArray[2];

                    if (day1.length < 2) {
                        day1 = '0' + day1;
                    }
                    if (month1.length < 2) {
                        month1 = '0' + month1;
                    }
                    if (day2.length < 2) {
                        day2 = '0' + day2;
                    }
                    if (month2.length < 2) {
                        month2 = '0' + month2;
                    }
                    var date1 = new Date(year1, month1 - 1, day1);
                    var date2 = new Date(year2, month2 - 1, day2);
                    if (date1 > date2) {
                        alert('Date Could Not be less than From Date');
                        document.getElementById(txtFromdate).focus();
                        flag = false;

                    }
                }
                else {
                    flag = false;
                }
            }
            return flag;
        }
        function isDate(txtDateId) {
            // date length should be 10 characters - no more, no less            
            var Ctrl = document.getElementById(txtDateId);
            var txtDate = Ctrl.value;
            var flag = true;

            // extract day, month and year from the txtDate string
            // expected format is dd/mm/yyyy
            var day = '';
            var month = '';
            var year = '';
            var dateArray = new Array();
            var count = 0;
            for (var j = 0; j < txtDate.length; j++) {
                var chara = txtDate.charAt(j);
                if (chara == '/') {
                    count += 1;
                }
            }
            if (count != 2) {
                alert('Invalid date !');
                return false;
            }
            dateArray = txtDate.split("/");


            day = dateArray[0];
            month = dateArray[1];
            year = dateArray[2];


            if (year.length != 4) {
                alert('Invalid date !');
                return false;
            }
            var finYear = document.getElementById("hidFinyear").value;
            if (month >= 4 && year != parseInt(finYear)) {
                alert('Date Should be between 01/04/' + finYear + ' and 31/03/' + (parseInt(finYear) + 1));
                return false;
            }
            else if (month < 4 && year != (parseInt(finYear) + 1)) {
                alert('Date Should be between 01/04/' + finYear + ' and 31/03/' + (parseInt(finYear) + 1));
                return false;
            }



            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                if (day > 31) {
                    alert('Date Should not be Greater than 31');
                    Ctrl.focus();
                    flag = false;
                }
            }
            //for have only 30 day
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day > 30) {
                    alert('Date Should not be Greater than 30');
                    Ctrl.focus();
                    flag = false;
                }
            }
            //for Febuary 
            else if (month == 2) {
                var leap = year % 4;
                if (leap == 0) {
                    if (day > 29) {
                        alert('february have only 29 days');
                        Ctrl.focus();
                        flag = false;
                    }
                }
                else {
                    if (day > 28) {
                        alert('february have only 28 days');
                        Ctrl.focus();
                        flag = false;
                    }
                }
            }
            else if (month > 12) {
                alert('Invalid Date');
                Ctrl.focus();
                flag = false;
            }
            // otherwise return true
            return flag;
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <acc:heading ID="pagehead" runat="server" HeadingText="Stock Ledger" />
    
    <center>
        <asp:Label ID="lblMsg" runat="server" ForeColor="#f29e08" Font-Bold="true"></asp:Label><br />
        <br />
    </center>
    <asp:Panel ID="pnlOption" runat="server" style="width: 50%; margin-left:25%; margin-right: 5%;">
                    <table style="width: 100%;" class="tablecontent">
                          <tr>
                            <td>
                                <asp:GridView ID="grdStockLedger" runat="server" CssClass="gridview" 
                                    DataKeyNames="ITMID" Width="100%" AllowPaging="True" AutoGenerateColumns="False" 
                                    OnPageIndexChanging="grdStockLedger_OnPageIndexChanging">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Select">
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chkOpt" runat="server" onclick="CheckSelectAll();" />
                                            </ItemTemplate>
                                            <ItemStyle Width="20px" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Items">
                                            <ItemTemplate>
                                                <asp:Label ID="lblITMDESC" runat="server" Text='<%#Eval("ITMDESC") %>'></asp:Label>
                                                <asp:HiddenField ID="hidITMID" runat="server" Value='<%#Eval("ITMID") %>' />
                                            </ItemTemplate>
                                            <ItemStyle Width="400px" />
                                        </asp:TemplateField>
                                    </Columns>
                                   
                                    <EmptyDataTemplate>
                                        <center>
                                            <span style="font-weight: bold; color: Red;">No Record Found !</span>
                                        </center>
                                    </EmptyDataTemplate>
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                    <table width="100%" style="padding-top: 10px;">
                            <tr>
                                <td align="left" style="padding-top: 10px;">
                                    <asp:CheckBox ID="ChkSelectAll" runat="server" Text="Select All" 
                                        onclick="SelectAll(this.id);"  />
                                </td>
                                <td align="right" style="padding-top: 10px;">
                                    <asp:Button ID="btnDisplay" runat="server" Text="Display" CssClass="button" OnClientClick="return checkselected();"
                                        OnClick="btnDisplay_Click" />
                                </td>
                                <td align="left" style="padding-top: 10px;">
                                    &nbsp;&nbsp;<asp:Button ID="btnCancel" runat="server" CssClass="button" Text="Cancel"
                                        OnClick="btnCancel_Click" />
                                </td>
                            </tr>
                        </table>
    </asp:Panel>
    <asp:Panel ID="pnlGrid" runat="server" Width="98%" Visible="false" style="margin-left:1%;">
    <asp:DataList ID="dtlstStockDetail" runat="server" OnItemDataBound="dtlstStockDetail_ItemBound" Width="96%" ItemStyle-BorderColor="#818181" ItemStyle-BorderStyle="Solid" ItemStyle-BorderWidth="1px">
    <ItemTemplate>
    <asp:ImageButton runat="server" ID="imgShow" ImageUrl="../../../../Images/plus.png" AlternateText="plus" />
    <asp:Label ID="lblLedgerName" runat="server" Text='<%#Eval("LName") %>'></asp:Label>
    <asp:HiddenField ID="hidItemID" runat="server" Value='<%#Eval("Itemid") %>' />
    <asp:GridView ID="grdDetailsfinal"  Width="100%" CssClass="gridview" AutoGenerateColumns="false" style=" display:none; margin-top:5px;" runat="server">
    <Columns>                   
            <asp:BoundField HeaderText="Tran. Date" DataField="EntryDate" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Width="10%" />
            <asp:BoundField HeaderText="Tran. Type" DataField="TRANTYPE" ItemStyle-Width="8%" />
            <asp:BoundField HeaderText="Customer/Supplier" DataField="Customer"  ItemStyle-Width="15%" />
            <asp:BoundField HeaderText="Tran. No." DataField="TRANNUMBER" ItemStyle-Width="18%" />
    </Columns>
                <EmptyDataTemplate></EmptyDataTemplate>
                </asp:GridView>
    </ItemTemplate>
    </asp:DataList>
    
    </asp:Panel>
    <asp:Panel ID="pnlChangeDate" runat="server" Width="350px" Height="160px" Style="display: none"
        CssClass="modalPopup">
        <div style="width: 80%; margin: 20px 10% 10px 10%;">
            <table>
                <tr>
                    <td>
                        From Date
                    </td>
                    <td style="padding-left: 10px">
                        <asp:TextBox ID="txtFromDate" runat="server" CssClass="input" ToolTip="DD/MM/YYYY"
                            Width="100px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        To Date
                    </td>
                    <td style="padding-left: 10px">
                        <asp:TextBox ID="txtToDate" runat="server" CssClass="input" ToolTip="DD/MM/YYYY"
                            Width="100px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btnCangeDate" runat="server" CssClass="button" Text="Ok" OnClick="btnCangeDate_Click"
                            OnClientClick="return Validation();" />&nbsp;
                        <asp:Button ID="Button1" runat="server" CssClass="button" Text="Cancel" />&nbsp;
                    </td>
                </tr>
            </table>
        </div>
    </asp:Panel>
    </form>
</body>

<script type="text/javascript" language="javascript">

  
    function SelectAll(id) {

        //get reference of GridView control
        var grid = document.getElementById("<%= grdStockLedger.ClientID %>");

        //variable to contain the cell of the grid
        var cell;
        if (grid.rows.length > 0) {
            //loop starts from 1. rows[0] points to the header.
            for (i = 1; i < grid.rows.length; i++) {
                //get the reference of first column
                cell = grid.rows[i].cells[0];

                //loop according to the number of childNodes in the cell
                for (j = 0; j < cell.childNodes.length; j++) {
                    //if childNode type is CheckBox
                    if (cell.childNodes[j].type == "checkbox") {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                        cell.childNodes[j].checked = document.getElementById(id).checked;
                    }
                }
            }
        }
    }

      function CheckSelectAll() {
        var countchecked = 0;
                //get reference of GridView control
        var grid = document.getElementById("<%= grdStockLedger.ClientID %>");

        //variable to contain the cell of the grid
        var cell;
        if (grid.rows.length > 0) {
            //loop starts from 1. rows[0] points to the header.
            for (i = 1; i < grid.rows.length; i++) {
                //get the reference of first column
                cell = grid.rows[i].cells[0];

                //loop according to the number of childNodes in the cell
                for (j = 0; j < cell.childNodes.length; j++) {
                    //if childNode type is CheckBox
                    if (cell.childNodes[j].type == "checkbox") {
                        //assign the status of the Select All checkbox to the cell checkbox within the grid
                        if (cell.childNodes[j].checked == true) {
                            countchecked = countchecked + 1;
                        }
                    }
                }
            }
            if (countchecked + 2 == grid.rows.length) {
                document.getElementById("ChkSelectAll").checked = true;
            }
            else {
                document.getElementById("ChkSelectAll").checked = false;
            }
        }
    }
   
    function checkselected() {
        var flag = true;
        var checkedcount = 0;
        var grid = document.getElementById("<%= grdStockLedger.ClientID %>");
        var cell;
        if (grid.rows.length > 0) {
            for (i = 1; i < grid.rows.length; i++) {
                cell = grid.rows[i].cells[0];
                for (j = 0; j < cell.childNodes.length; j++) {
                    if (cell.childNodes[j].type == "checkbox") {
                        if (cell.childNodes[j].checked == true) {
                            checkedcount = checkedcount + 1;
                        }
                    }
                }
            }
        }
        if (checkedcount <= 0) {
            alert("Please select a customer from list to display details !!");
            flag = false;
        }
        return flag;
    }
</script>

</html>
